<?php
namespace Home\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Crypt\Password\Bcrypt;
use Zend\Session\Container;
use Zend\Http\Client;
use Zend\Db\Sql\Where;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Admin\Model\Video;
use Zend\Db\Sql\Sql;

class VideoTable extends AbstractTableGateway
{
    protected $table = 'tbl_video';

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Video());
        $this->initialize();
    }

    public function getbytitle($str)
    {
    	$sql = new Sql($this->adapter);
	    $select = $sql->select();
	    $select->from ('tbl_video');
	    $where= new Where();
        $where->like('title','%'.$str.'%');
        $select->where(array($where,'status'=>'1') );
        $select->order('video_id DESC');
	    $selectString = $sql->prepareStatementForSqlObject($select);
            //return $selectString;die;
	    $results = $selectString->execute();

	    
    	$resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new Video());
        $paginatorAdapter = new DbSelect(
            $select,
            $this->getAdapter(),
            $resultSetPrototype
            );
        $Paginator =  new Paginator($paginatorAdapter);
        return $Paginator;
    }

    public function getvideobyid($id)
    {
    	$cate = array();
    	$resultSet = $this->select(array('video_id'=>$id,'status'=>'1'));
        foreach ($resultSet as $value) 
        {
            $cate[] = $value;
        }
        return $cate;
    }
    public function getlimitvideo($offset,$limit)
    {
    	$sql = new Sql($this->adapter);
	    $select = $sql->select();
	    $select->from ('tbl_video');
	    $select->where(array('status'=>'1'));
	    $select->order('video_id DESC');
	    $select->offset($offset);
	    $select->limit($limit); // always takes an integer/numeric
	    $selectString = $sql->prepareStatementForSqlObject($select);
	    $results = $selectString->execute();
		$array = array();
		foreach($results as $v)
		{
			$array[] = $v;
		}
		return $array;
    }


    public function getbyCate($id)
    {
    	$sql = new Sql($this->adapter);
	     $select = $sql->select();
	     $select->columns(array('name'=>'name'));
	     $select->from ('tbl_categories')
	            ->join('tbl_video', 'tbl_video.categories_id=tbl_categories.categories_id',array('categories_id'=>'categories_id','url'=>'url','title'=>'title','video_id'=>'video_id','image'=>'image'));
	     $select->where(array('tbl_categories.categories_id'=>$id));
	     $select->order('video_id DESC');
	     $selectString = $sql->prepareStatementForSqlObject($select);
	     $results = $selectString->execute();
	     $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Video());
            //tạo ra một bộ chuyển đổi mới pagination đối tượng
            $paginatorAdapter = new DbSelect(
                // cấu hình chọn đối tượng
                $results,
                //các bộ chuyển đổi để chạy nguoc lại
                $this->getAdapter(),
                $resultSetPrototype
                );
            $Paginator =  new Paginator($paginatorAdapter);
            

            return $Paginator;
	}


	//***************************Code by nguyen hong quan************************************//
}